﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Takkou.Core;

namespace Takkou
{
    public partial class GameScreen : UserControl
    {
        private const double SECOND = 10000000; // number of clock ticks per second
        private long lastTime = 0;
        private bool running = false;
		
        private Mission mission;

        // Public properties
        public int CellWidth { get { return (int)(WorldArea.Width / mission.World.Width); } }
        public int CellHeight { get { return (int)(WorldArea.Height / mission.World.Height); } }

        public long ElapsedTime { get; private set; }
        public double FrameRate { get { return (ElapsedTime / SECOND) * Veloc.Value; } }

        // Singleton instance
        private static GameScreen instance = null;
        public static GameScreen Instance { get { return instance; } }

        public GameScreen(Mission mis)
        {
            mission = mis;
            InitializeComponent();
            Loaded += new RoutedEventHandler(Load);
            instance = this;
		}

		public void Load(object o, RoutedEventArgs sender)
		{
            CreateTerrain();
            foreach (WorldElement elm in mission.World.getElements())
            {
                WorldArea.Children.Add(elm.View);
            }
            CompositionTarget.Rendering += new System.EventHandler(Update);
		}

        private void Update(object sender, System.EventArgs e)
        {
            RenderingEventArgs r = e as RenderingEventArgs;
            if (running)
            {
                ElapsedTime = r.RenderingTime.Ticks - lastTime;
                foreach (WorldElement elm in mission.World.getElements())
                {
                    elm.update();
                }
            }
            lastTime = r.RenderingTime.Ticks;
        }

        private void PlayPause(object sender, RoutedEventArgs e)
        {
            if (running)
            {
                playPause.Content = "Play";
            }
            else
            {
                playPause.Content = "Pause";
            }
            running = !running;
        }

        private void CreateTerrain()
        {
            for (int i = 0; i < mission.World.Height; i++)
                WorldArea.RowDefinitions.Add(new RowDefinition { Height = new GridLength(CellHeight) });

            for (int i = 0; i < mission.World.Width; i++)
                WorldArea.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(CellWidth) });

            for (int y = 0; y < mission.World.Height; y++)
                for (int x = 0; x < mission.World.Width; x++)
                {
                    UIElement block = null;
                    switch (mission.World.terrainAt(x,y))
                    {
                        case TerrainType.GRASS:
                            block = new GrassBlock();
                            break;
                        case TerrainType.STONE:
                            block = new StoneBlock();
                            break;
                        case TerrainType.WATER:
                            block = new WaterBlock();
                            break;
                    }
                    block.SetValue(Grid.RowProperty, y);
                    block.SetValue(Grid.ColumnProperty, x);
                    WorldArea.Children.Add(block);
                }
        }
    }
}
